package com.zzw.thread;

public class AccountSyn {

	private String accountNo;
	private double balance;
	private boolean flag = false;
	public AccountSyn(){
		
	}
	
	public AccountSyn(String accountNo,double balance){
		this.accountNo = accountNo;
		this.balance = balance;
	}
	
	public synchronized void draw(double drawAmount){
		try {
			if(!flag){
				wait();
			}
			else{
				System.out.println(Thread.currentThread().getName() + 
						"ȡǮ:" + drawAmount);
				balance -= drawAmount;
				System.out.println("" + balance);
				flag = false;
				notifyAll();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public synchronized void deposit(double depositAmount) throws InterruptedException{
		if(flag){
			wait();
		}
		else{
			System.out.println(Thread.currentThread().getName() + 
					"" + depositAmount);
			balance += depositAmount;
			System.out.println("" + balance);
			flag = true;
			notifyAll();
		}
	}
	
	@Override
	public int hashCode() {
		return accountNo.hashCode();
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj){
			return true;
		}
		else if(obj != null && obj.getClass() == Account.class){
			Account target = (Account) obj;
			return target.getAccountNo().equals(accountNo);
		}
		return false;
	}
	
	public String getAccountNo() {
		return accountNo;
	}
	
	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}

	public double getBalance() {
		return balance;
	}
}